package com.ni.learning.exam.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ni.learning.exam.dto.AnswerInfoDTO;
import com.ni.learning.exam.mapper.AnswerInfoMapper;
import com.ni.learning.exam.mapper.AnswerSheetMapper;
import com.ni.learning.exam.service.AnswerSheetService;
import com.ni.learning.exam.vo.AnswerSheetVO;
import com.our.learning.infrastructure.constant.ResponseStatusEnum;
import com.our.learning.infrastructure.po.AnswerInfo;
import com.our.learning.infrastructure.po.AnswerSheet;
import com.our.learning.infrastructure.pojo.Response;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author : Ice'Clean
 * @date : 2023-06-24
 */
@Service
public class AnswerSheetServiceImpl implements AnswerSheetService {

    private final AnswerSheetMapper answerSheetMapper;
    private final AnswerInfoMapper answerInfoMapper;

    public AnswerSheetServiceImpl(AnswerSheetMapper answerSheetMapper, AnswerInfoMapper answerInfoMapper) {
        this.answerSheetMapper = answerSheetMapper;
        this.answerInfoMapper = answerInfoMapper;
    }

    @Override
    public AnswerSheet getAnswerSheetById(int sheetId) {
        return answerSheetMapper.selectById(sheetId);
    }

    @Override
    public AnswerSheet getAnswerSheetByStudentExam(int studentId, int examId) {
        return answerSheetMapper.selectOne(new LambdaQueryWrapper<AnswerSheet>()
                .eq(AnswerSheet::getStudentId, studentId)
                .eq(AnswerSheet::getExamId, examId));
    }

    @Override
    public AnswerSheet getAnswerSheetByStudentPaper(int studentId, int paperId) {
        return answerSheetMapper.selectOne(new LambdaQueryWrapper<AnswerSheet>()
                .eq(AnswerSheet::getStudentId, studentId)
                .eq(AnswerSheet::getPaperId, paperId));
    }

    @Override
    public List<AnswerSheet> getAnswerSheetByStudent(int studentId) {
        return answerSheetMapper.selectList(new LambdaQueryWrapper<AnswerSheet>()
                .eq(AnswerSheet::getStudentId, studentId));
    }

    @Override
    public AnswerSheet createAnswerSheet(int studentId, int examId, int paperId) {
        AnswerSheet answerSheet = new AnswerSheet();
        answerSheet.setExamId(examId);
        answerSheet.setStudentId(studentId);
        answerSheet.setPaperId(paperId);
        answerSheetMapper.insert(answerSheet);
        return answerSheet;
    }

    @Override
    public List<Integer> getIncorrectIds(int studentId, Integer paperId) {
        // 查询学生答题卡
        List<AnswerSheet> answerSheetList;
        if (paperId != null) {
            AnswerSheet answerSheet = getAnswerSheetByStudentPaper(studentId, paperId);
            if (answerSheet != null) {
                answerSheetList = new ArrayList<>(1);
                answerSheetList.add(answerSheet);
            } else {
                answerSheetList = Collections.emptyList();
            }
        } else {
            answerSheetList = getAnswerSheetByStudent(studentId);
        }
        if (answerSheetList.isEmpty()) {
            return Collections.emptyList();
        }
        List<Integer> sheetIdList = answerSheetList.stream()
                .map(AnswerSheet::getId).collect(Collectors.toList());
        // 查询所有错题
        List<AnswerInfo> answerInfos = answerInfoMapper.selectList(new LambdaQueryWrapper<AnswerInfo>()
                .select(AnswerInfo::getQuestionId)
                .in(AnswerInfo::getSheetId, sheetIdList)
                .apply("score < sum_score"));
        return answerInfos.stream().map(AnswerInfo::getQuestionId).collect(Collectors.toList());
    }

    @Override
    public Response submitAnswer(AnswerInfoDTO answerInfoDTO) {
        AnswerInfo answerInfo = answerInfoMapper.selectOne(new LambdaQueryWrapper<AnswerInfo>()
                .eq(AnswerInfo::getSheetId, answerInfoDTO.getSheetId())
                .eq(AnswerInfo::getTitleNo, answerInfoDTO.getTitleNo()));
        if (answerInfo != null) {
            // 原先提交过，修改答案
            AnswerInfo answerInfo1 = answerInfoDTO.getAnswerInfo();
            answerInfo1.setId(answerInfo.getId());
            answerInfoMapper.updateById(answerInfo1);
        } else {
            // 原先没有，第一次提交
            answerInfoMapper.insert(answerInfoDTO.getAnswerInfo());
        }

        return new Response(ResponseStatusEnum.OK);
    }

    @Override
    public AnswerInfo getSheetAnswer(int sheetId, int titleNo) {
        return answerInfoMapper.selectOne(new LambdaQueryWrapper<AnswerInfo>()
                .eq(AnswerInfo::getSheetId, sheetId)
                .eq(AnswerInfo::getTitleNo, titleNo));
    }

    @Override
    public Response submitCheck(AnswerInfoDTO answerInfoDTO) {
        AnswerInfo answerInfo = getSheetAnswer(answerInfoDTO.getSheetId(), answerInfoDTO.getTitleNo());
        answerInfo.setScore(answerInfoDTO.getScore());
        answerInfoMapper.updateById(answerInfo);
        return new Response(ResponseStatusEnum.OK);
    }

    @Override
    public Response sheetList(int examId) {
        List<AnswerSheet> answerSheetList = answerSheetMapper.selectList(new LambdaQueryWrapper<AnswerSheet>()
                .eq(AnswerSheet::getExamId, examId));
        List<AnswerSheetVO> answerSheetVOList = answerSheetList.stream().map(AnswerSheetVO::new).collect(Collectors.toList());
        return new Response(ResponseStatusEnum.OK)
                .addData("answerSheetList", answerSheetVOList);
    }

    @Override
    public Response submitCheckResult(int sheetId) {
        // 查询出答题卡
        AnswerSheet answerSheet = getAnswerSheetById(sheetId);
        // 查询所有答案信息，累加得到总分，客观题自动记分
        List<AnswerInfo> answerInfoList = answerInfoMapper.selectList(new LambdaQueryWrapper<AnswerInfo>()
                .eq(AnswerInfo::getSheetId, sheetId));
        int sumScore = 0;
        for (AnswerInfo info : answerInfoList) {
            // 可能是客观题，自动查分。不为空表示教师已评分的主观题
            if (info.getScore() == null) {
                // info.setScore(autoCheck(info.getQuestionId()));
                if (info.getSumScore() == null) {
                    info.setSumScore(20);
                }
                info.setScore(info.getSumScore());
            }
            // 累计总分
            sumScore += info.getScore();
        }
        // 总分赋值
        answerSheet.setScore(sumScore);
        // 修改批改标识
        answerSheet.setChecked(1);
        answerSheetMapper.updateById(answerSheet);
        return new Response(ResponseStatusEnum.OK)
                .addData("answerSheet", answerSheet);
    }
}
